home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / Line.java < prev    next >
Text File  |  1998-08-21  |  9KB  |  322 lines

  1. package symantec.itools.awt.shape;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Polygon;
  5. import java.awt.Dimension;
  6. import java.beans.PropertyVetoException;
  7. import java.beans.PropertyChangeListener;
  8. import java.beans.VetoableChangeListener;
  9.  
  10. //  07/31/97    LAB    Created, implemented and checked in.
  11. //  08/13/97    LAB    Added getPreferredSize which returns a 50 by 50 dimension and getMinimumSize
  12. //                which returns a 10 by 10 dimension, just like the Shape class. Addresses Mac
  13. //                Bug #7138.  Changed the default slope to negative.  Addresses Mac Bug #7193
  14. //  08/29/97    CAR modified getPreferredSize
  15.  
  16. /**
  17.  * Implements a line than can have a thickness.
  18.  * @version 1.1, July 30, 1997
  19.  * @author Symantec
  20.  */
  21. public class Line extends java.awt.Component
  22. {
  23.     /**
  24.      * Constructs a default Line with positive slope and thickness of 1 pixel.
  25.      */
  26.     public Line()
  27.     {
  28.         slopeType        = false;
  29.         lineThickness    = 1;
  30.     }
  31.  
  32.     /**
  33.      * Sets the slope type of the line to draw.
  34.      * @param type if the type is true the line will be drawn from
  35.      * the lowwer left corner to the upper right corner.  If the type is false,
  36.      * the line will be drawn from the upper left corner to the bottom right corner.
  37.      * @exception PropertyVetoException
  38.      * if the specified property value is unacceptable
  39.      * @see #isPositiveSlope
  40.      */
  41.     public void setPositiveSlope(boolean type) throws PropertyVetoException
  42.     {
  43.         if (slopeType != type)
  44.         {
  45.             Boolean oldValue = new Boolean(slopeType);
  46.             Boolean newValue = new Boolean(type);
  47.  
  48.             vetos.fireVetoableChange("PositiveSlope", oldValue, newValue);
  49.  
  50.             slopeType = type;
  51.             repaint();
  52.  
  53.             changes.firePropertyChange("PositiveSlope", oldValue, newValue);
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Gets the slope type of the line to draw.
  59.      * @return true if the line will be drawn from the lowwer left corner to the
  60.      * upper right corner.  false if the line will be drawn from the upper left
  61.      * corner to the bottom right corner.
  62.      * @see #setPositiveSlope
  63.      */
  64.     public boolean isPositiveSlope()
  65.     {
  66.         return slopeType;
  67.     }
  68.  
  69.     /**
  70.      * Sets the thikcness of the line to draw.
  71.      * @param thickness the thickness of the line in pixels
  72.      * @exception PropertyVetoException
  73.      * if the specified property value is unacceptable
  74.      * @see #getLineThickness
  75.      */
  76.     public void setLineThickness(int thickness) throws PropertyVetoException
  77.     {
  78.         if (lineThickness != thickness)
  79.         {
  80.             Integer oldValue = new Integer(lineThickness);
  81.             Integer newValue = new Integer(thickness);
  82.  
  83.             vetos.fireVetoableChange("LineThickness", oldValue, newValue);
  84.  
  85.             lineThickness = thickness;
  86.             repaint();
  87.  
  88.             changes.firePropertyChange("LineThickness", oldValue, newValue);
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Gets the thickness of the line to draw.
  94.      * @return the thickness of the line in pixels
  95.      * @see #setLineThickness
  96.      */
  97.     public int getLineThickness()
  98.     {
  99.         return lineThickness;
  100.     }
  101.  
  102.     /**
  103.      * Returns the minimum dimensions to properly display this component.
  104.      * This is a standard Java AWT method which gets called to determine
  105.      * the minimum size of this component.
  106.      * Returns the results of a call to getPreferredSize.
  107.      * @see #getPreferredSize
  108.      */
  109.     public Dimension getMinimumSize()
  110.     {
  111.         return new Dimension(10, 10);
  112.     }
  113.  
  114.     /**
  115.      * Returns the recommended dimensions to properly display this component.
  116.      * This is a standard Java AWT method which gets called to determine
  117.      * the recommended size of this component.
  118.      *
  119.      * @return horiziontal and vertical dimensions of 50, 50
  120.      *
  121.      * @see java.awt.Component#minimumSize
  122.      */
  123.     public Dimension getPreferredSize()
  124.     {
  125.         Dimension dim = size();
  126.         Dimension min = getMinimumSize();
  127.  
  128.         return new Dimension(Math.max(dim.width, min.width), Math.max(dim.height, min.height));
  129.     }
  130.  
  131.     /**
  132.      * Adds a listener for all property change events.
  133.      * @param listener the listener to add
  134.      * @see #removePropertyChangeListener
  135.      */
  136.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  137.     {
  138.         changes.addPropertyChangeListener(listener);
  139.     }
  140.  
  141.     /**
  142.      * Removes a listener for all property change events.
  143.      * @param listener the listener to remove
  144.      * @see #addPropertyChangeListener
  145.      */
  146.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  147.     {
  148.         changes.removePropertyChangeListener(listener);
  149.     }
  150.  
  151.     /**
  152.      * Adds a listener for all vetoable property change events.
  153.      * @param listener the listener to add
  154.      * @see #removeVetoableChangeListener
  155.      */
  156.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  157.     {
  158.         vetos.addVetoableChangeListener(listener);
  159.     }
  160.  
  161.     /**
  162.      * Removes a listener for all vetoable property change events.
  163.      * @param listener the listener to remove
  164.      * @see #addVetoableChangeListener
  165.      */
  166.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  167.     {
  168.         vetos.removeVetoableChangeListener(listener);
  169.     }
  170.  
  171.     /**
  172.      * Checks whether this component "contains" the specified (x, y)
  173.      * location, where x and y are defined to be relative to the
  174.      * coordinate system of this component.
  175.      * @param x the x coordinate
  176.      * @param y the y coordinate
  177.      * @see java.awt.Component#getComponentAt
  178.      */
  179.     public boolean contains(int x, int y)
  180.     {
  181.         if(!super.contains(x, y))
  182.             return false;
  183.  
  184.         if (lineThickness > 1)
  185.         {
  186.             if (poly != null)
  187.                 return poly.contains(x, y);
  188.             else
  189.                 return false;
  190.         }
  191.         else
  192.         {
  193.             double x1, y1, x2, y2, i = 0;
  194.             double calc;
  195.  
  196.             Dimension s = getSize();
  197.  
  198.             if(slopeType)
  199.             {
  200.                 x1 = s.width;
  201.                 y1 = 0;
  202.                 x2 = 0;
  203.                 y2 = s.height;
  204.             }
  205.             else
  206.             {
  207.                 x1 = s.width;
  208.                 y1 = s.height;
  209.                 x2 = 0;
  210.                 y2 = 0;
  211.             }
  212.  
  213.             if(s.width > s.height)
  214.             {
  215.                 calc = Math.abs( (((y1 - y2) * (x - x2)) / (x1 - x2)) + y2 );
  216.                 if (Math.abs(y - calc) < 0.7)
  217.                     return true;
  218.                 else
  219.                     return false;
  220.             }
  221.             else
  222.             {
  223.                 calc = Math.abs( (((x1 - x2) * (y - y2)) / (y1 - y2)) + x2 );
  224.                 if (Math.abs(x - calc) < 0.7)
  225.                     return true;
  226.                 else
  227.                     return false;
  228.             }
  229.         }
  230.     }
  231.  
  232.     /**
  233.      * Paints the line using the given graphics context.
  234.      * This is a standard Java AWT method which typically gets called
  235.      * by the AWT to handle painting this component. It paints this component
  236.      * using the given graphics context. The graphics context clipping region
  237.      * is set to the bounding rectangle of this component and its <0,0>
  238.      * coordinate is this component's top-left corner.
  239.      *
  240.      * @param g the graphics context used for painting
  241.      * @see java.awt.Component#repaint
  242.      * @see java.awt.Component#update
  243.      */
  244.     public void paint(Graphics g)
  245.     {
  246.         int x1, y1, x2, y2, i = 0;
  247.  
  248.         Dimension s = getSize();
  249.  
  250.         if(slopeType)
  251.         {
  252.             x1 = s.width;
  253.             y1 = 0;
  254.             x2 = 0;
  255.             y2 = s.height;
  256.         }
  257.         else
  258.         {
  259.             x1 = s.width;
  260.             y1 = s.height;
  261.             x2 = 0;
  262.             y2 = 0;
  263.         }
  264.  
  265.         g.setColor(getForeground());
  266.         if(lineThickness > 1)
  267.         {
  268.             double w        = lineThickness/2;
  269.             double theta    = 3.1416 - (1.5708 + Math.abs(Math.atan((double)(y1 - y2)/(x1 - x2))));
  270.             double a        = Math.abs(w * Math.cos(theta));
  271.             double b        = Math.abs(w * Math.sin(theta));
  272.             int A1x, B1y, A2x, B2y;
  273.  
  274.             if(slopeType)
  275.             {
  276.                 A1x = (int) (x1 - a - a);
  277.                 B1y = (int) (y1 + b + b);
  278.  
  279.                 A2x = (int) (x2 + a + a);
  280.                 B2y = (int) (y2 - b - b);
  281.             }
  282.             else
  283.             {
  284.                 A1x = (int) (x1 - a - a);
  285.                 B1y = (int) (y1 - b - b);
  286.  
  287.                 A2x = (int) (x2 + a + a);
  288.                 B2y = (int) (y2 + b + b);
  289.             }
  290.             poly = new Polygon();
  291.             poly.addPoint(A1x, (int) y1);
  292.             poly.addPoint((int) x1, B1y);
  293.             poly.addPoint(A2x, (int) y2);
  294.             poly.addPoint((int) x2, B2y);
  295.             g.fillPolygon(poly);
  296.         }
  297.         else
  298.         {
  299.             g.drawLine(x1, y1, x2, y2);
  300.         }
  301.     }
  302.  
  303.     /**
  304.      * The slope type of the line to be drawn (either positive (true) or negative (false)).
  305.      */
  306.     protected boolean slopeType;
  307.  
  308.     /**
  309.      * The thickness of the line to draw.
  310.      */
  311.      protected int lineThickness;
  312.  
  313.     /**
  314.      * The polygon that defines the line when lineThickness is larger than 1.
  315.      */
  316.     protected Polygon poly = null;
  317.  
  318.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  319.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  320. }
  321.  
  322.